package cn.yong.service;

import cn.yong.dao.EmpDao;
import cn.yong.entity.Dept;
import cn.yong.entity.Emp;
import cn.yong.entity.EmpCondition;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import java.util.Iterator;
import java.util.List;

public class EmpService {

    EmpDao dao = new EmpDao();

    public List<Emp> getList(){
        Transaction tx = null;
        List<Emp> list = null;
        try {
            tx = dao.currentSession().beginTransaction();
            list = dao.findList();
            tx.commit();
        }catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (tx!=null) {
                tx.rollback();
            }
        }

        return list;
    }


    public Iterator<Emp> findIterate(){
        Iterator<Emp> iterator= null;
        Transaction tx = null;

        try {
            tx = dao.currentSession().beginTransaction();

            iterator = dao.findIterae();
            //遍历并输出结果，与调用list()方法时不同，须在会话关闭前测试查询效果
            Emp emp = null;
            while (iterator.hasNext()){
                emp = iterator.next();
                System.out.println(emp.getEname());
            }

            tx.commit();
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (tx!=null) {
                tx.rollback();
            }
        }
        return iterator;
    }

    public List<Emp> findEmpsByConditions(Object[] conditions) {
        Transaction tx = null;
        List<Emp> emps = null;
        try {
            tx = dao.currentSession().beginTransaction(); // (1)开始事务
            emps = dao.findByConditions(conditions); // (2)持久化操作
            tx.commit(); // (3)提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // (4)回滚事务
        }
        return emps;
    }

    public List<Emp> findEmpsByConditions(Emp conditions) {
        Transaction tx = null;
        List<Emp> emps = null;
        try {
            tx = dao.currentSession().beginTransaction(); // (1)开始事务
            emps = dao.findByConditions(conditions); // (2)持久化操作
            tx.commit(); // (3)提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // (4)回滚事务
        }
        return emps;
    }

    public List<Emp> findEmpsByConditions(EmpCondition conditions) {
        Transaction tx = null;
        List<Emp> emps = null;
        try {
            tx = dao.currentSession().beginTransaction(); // 开启事务
            // HQL根据条件动态生成
            StringBuilder hql = new StringBuilder("from Emp as emp where 1=1");

            if (conditions.getJob() != null && conditions.getJob().length() > 0) {
                hql.append(" and emp.job = :job");
            }
            if (conditions.getSalary() != null && conditions.getSalary() != 0) {
                hql.append(" and emp.sal > :salary");
            }
            if (null != conditions.getHireDateStart()) {
                hql.append(" and emp.hiredate > :hireDateStart");
            }
            if (null != conditions.getHireDateEnd()) {
                hql.append(" and emp.hiredate < :hireDateEnd");
            }
            emps = new EmpDao().findByConditions(hql.toString(), conditions);
            tx.commit(); // 提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // 回滚事务
        }
        return emps;
    }

    public Long countBySalary(Double sal) {
        Transaction tx = null;
        Long result = null;
        try {
            tx = dao.currentSession().beginTransaction(); // 开启事务
            result = dao.obtainTheRowCount(sal);
            tx.commit(); // 提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // 回滚事务
        }
        return result;
    }

    public List<Emp> findEmpsByPage(int pageNo, int pageSize) {
        Transaction tx = null;
        List<Emp> emps = null;
        try {
            tx = dao.currentSession().beginTransaction(); // 开启事务
            emps = new EmpDao().findByPage((pageNo - 1) * pageSize, pageSize);
            tx.commit(); // 提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // 回滚事务
        }
        return emps;
    }

    public void save(Emp emp){
        Transaction tx = null;

        try {
            tx = dao.currentSession().beginTransaction();

            dao.save(emp);

            tx.commit();
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
    }

    public List<Emp> findByInvisibleJoin(String deptName){
        Transaction tx = null;
        List<Emp> emps = null;
        try {
            tx = dao.currentSession().beginTransaction(); // 开启事务
            emps = new EmpDao().findByInvisibleJoin(deptName);
            tx.commit(); // 提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // 回滚事务
        }
        return emps;
    }

    public List<Object[]> findGroupBy(){
        Transaction tx = null;
        List<Object[]> result = null;
        try {
            tx = dao.currentSession().beginTransaction(); // 开启事务
            result = dao.findGroupBy();
            tx.commit(); // 提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback(); // 回滚事务
        }
        return result;
    }
}
